<?php

namespace App\Jobs;

use App\CouponJob;
use App\Repositories\CouponRepository;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class CouponSendJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $couponJob;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(CouponJob $couponJob)
    {
        $this->couponJob=$couponJob;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
       $couponRepository= new CouponRepository();
       User::chunk(100,function($user)use ($couponRepository){
           foreach ($user as $v){
               if($v->team_level_id==0&&$v->created_at<$this->couponJob->created_at){
                   $couponRepository->sendOne($v,$this->couponJob->coupon_id);
               }
           }
       });
       $this->couponJob->status=1;
       $this->couponJob->save();


    }
}
